var setting;
var lastValue = "", nodeList = [], fontCss = {};
var tloadinit;
allotRoleAuthority = {
	initZTree : function(rootNode){
		$.fn.zTree.init($("#"+rootNode), setting);
	},
	settingInit : function(posturl) {
		tloadinit = top.layer.load(0, {
			shade : [ 0.5, '#fff' ]
		}); // 0代表加载的风格，支持0-2
		setting = {
			check : {
				/** 复选框* */
				enable : true,
				chkboxType : {// 复选框
					"Y" : "",// 复选框
					"N" : ""// 复选框
				}
			},
			view : {
				dblClickExpand: false,
			},
			data : {
				simpleData : {// 数据是否采用简单 Array 格式，默认false
					enable : true,
					idKey : "id",
					pIdKey : "pid",
					rootPId : 0
				}
			},
			async : {
				enable : true,
				url : posturl,
				type : "post",
				contentType : "application/json"
			},
			callback : {
				onAsyncSuccess : allotRoleAuthority.zTreeOnAsyncSuccess,
				onAsyncError : allotRoleAuthority.zTreeOnAsyncError,
				beforeCheck: allotRoleAuthority.beforeCheck,
				onClick : allotRoleAuthority.onCheckBox
			}
		};
	},
	zTreeOnAsyncSuccess : function(event, treeId, treeNode){
    	allotRoleAuthority.initCheck();
    	top.layer.close(tloadinit);
    },
    zTreeOnAsyncError : function(event, treeId, treeNode){
    	
    },
    beforeCheck : function(treeId, treeNode) {
    	allotRoleAuthority.isBeforeCheckValue(treeNode);
		return (treeNode.check !== false);
	},
    onCheckBox : function(e, treeId, treeNode) {
    	allotRoleAuthority.isCheckValue(treeNode);
	},
	isCheckValue : function(treeNode){
		//选标题选值
		if(treeNode.pid!=null&&!treeNode.isParent){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			if(treeNode.checked!=undefined){
				zTree.checkNode(treeNode, !treeNode.checked, null, true);
			}
			var nodes = zTree.getCheckedNodes(true), v = "",ids="";
			for (var i = 0, l = nodes.length; i < l; i++) {
				ids += nodes[i].id+"#";
			}
			if (ids.length > 0)
				ids = ids.substring(0, ids.length - 1);
			$("#checkIds").val(ids);
		}
	},
	isBeforeCheckValue : function(treeNode){
		//选复选框
		var ids = $("#checkIds").val();
		if(!treeNode.checked){
			if(ids!=''){
				ids = ids +"#"+treeNode.id
			}else{
				ids = treeNode.id
			}
		}else{
			ids = functionUtil.replaceAllValue(functionUtil.replaceAllValue(functionUtil.replaceAllValue(ids,treeNode.id+"#",""),"#"+treeNode.id,""),treeNode.id,"");
		}
		$("#checkIds").attr("value",ids);
	},
	initCheck : function(){
		//初始化选中。
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = zTree.getCheckedNodes(true),ids="";
		for (var i = 0, l = nodes.length; i < l; i++) {
			ids += nodes[i].id+"#";
		}
		if (ids.length > 0)
			ids = ids.substring(0, ids.length - 1);
		$("#checkIds").attr("value",ids);
	},
	asyncJsonRoleAuthorityDoSubmitForum : function(){
		allotRoleAuthority.roleAuthorityDoSubmitForum();
		return true;
	},
	roleAuthorityDoSubmitForum : function() {
		var array = {};
		array['rid'] = $("#rid").val();
		array['id'] = $("#checkIds").val();
		var tload = top.layer.load(0, {
			shade : [ 0.5, '#fff' ]
		}); // 0代表加载的风格，支持0-2
		console.log(JSON.stringify(array));
		$.ajax({
			headers : {
				'Accept' : 'application/json',
				'Content-Type' : 'application/json'
			},
			type : "post",
			data : JSON.stringify(array),
			url :  'saveAllotRoleAuthorityTreeJson.do',
			async : true,
			dataType : "json"
		}).done(function(data) {
			// 成功回调，
			if (data != null) {
				top.layer.close(tload);
				$("#asyncResult").val(data.ret);
				$("#asyncResultError").val(data.msg);
			}
		}).fail(function() {
			// 失败回调
			top.layer.close(tload);
			$("#asyncResult").val(-110);
		});
	},
	asyncResult : function(){
		return $("#asyncResult").val();
	},
	asyncResultError : function(){
		return $("#asyncResultError").val();
	},
}
